﻿define([
    'views/baseView',
    'models/crud/projectModel',
    'text!templates/crud/mngBase.html',
    'text!templates/crud/mngTable.html',
    'text!templates/crud/project/projectList.html',
], function (BaseView, Model, BaseTmp, TableTmp, UniqTmp) {
    var view = BaseView.extend({

        $content: null,
        $table: null,

        render: function (element) {
            var that = this;
            toggleLoading(true);
            changeTopNavTitle("_projects");
            $("div[data-rel='crud_solution']").remove();
            $("div[data-rel='crud_project']").remove();

            var baseTemplate = Handlebars.compile(BaseTmp);
            var tableTemplate = Handlebars.compile(TableTmp);

            var dataBaseOptions = {
                uuId: that._uId,
                dataRel: "crud_project", //
                tmpClass: "project_manager", // 
                title: translate("_projectManager"),
                subtitle: translate("")
            };

            var dataTableOptions = {
                column: [{
                    colText: translate("_name"),
                    style: "width: 279px;",
                    ariasort: true,
                    ariaLabel: "_nameOfTheProject"
                }, {
                    colText: translate("_description"),
                    style: "width: 350px;",
                    ariasort: true,
                    ariaLabel: "_descriptionOfTheProject"
                }, {
                    colText: translate("_active"),
                    style: "width: 241px;",
                    ariasort: true,
                    ariaLabel: "_activateOrDeactivateSolution"
                }, {
                    colText: translate("_options"),
                    style: "width: 241px;",
                    ariasort: false,
                    ariaLabel: "_generalOptions"
                }]
            };

            var html = baseTemplate(dataBaseOptions);
            var html2 = tableTemplate(dataTableOptions);

            (new Model()).list(this.options.solutionId, function (oData) {
                var uniqTemplate = Handlebars.compile(UniqTmp);

                
                var dataUniqOptions = {
                    data: oData
                };

                // crud_tbody_content
                
                var html3 = uniqTemplate(dataUniqOptions);
                
                that.$el.append(html);
                that.$el.find(".table_container").append(html2);
                that.$el.find("tbody.crud_tbody_content").append(html3);

                that.$content = $("div[data-id='" + that._uId + "']");
                that.$table = that.$content.find("table");
                postRender(that.$el);
                
                that.$table.customTable();

                //$('<a href="#" class="btn btn-adduser"><i class="icon-plus"></i></a>').appendTo('div.data-table-options');

                that.addTableHandlers();
                that.addHandlers();

                toggleLoading(false);
            });
        },
        initialize: function(){

        },
        
        editPreferences: function (e) {
            var id = $(e.currentTarget).closest("tr").attr("data-id");

            require(['controllers/crud/solutionMngr'], function (Controller) {
                (new Controller()).showEditPref(id);
            });
        },
        deleteData: function (e) {
            var id = $(e.currentTarget).closest("tr").attr("data-id");

            require(['controllers/common/showModal'], function (Controller) {
                (new Controller()).show(
                    {
                        title: translate("_deleteSolution"),
                        text: translate("_sure_delete_solution"),
                        buttons: [
                            { "title": translate("yes"), "css": "primary", "code": "yes" },
                            { "title": translate("no"), "code": "no" }
                        ],
                        callback: function (code) {
                            if (code == "yes") {
                                var model = new Model();
                                //$("#mainLoading").show();
                                toggleLoading(true);
                                model.deleteData(id, function () {
                                    //$("#mainLoading").hide();
                                    toggleLoading(false);
                                    require(['controllers/crud/solutionMngr'], function (Controller) {
                                        (new Controller()).show();
                                    });
                                });
                            }
                        }
                    });
            });
        },
        changeState: function (e) {
            var userId = $(e.currentTarget).closest("tr").attr("data-id");
            var stateActual = $(e.currentTarget).val();
            var modelUser = new Model();
            // se puede usar el update y nadas mas.. <<<<<<<<<<<
            modelUser.getUser(userId, function (dataUser) {
                dataUser.active = !dataUser.active;
                var data = {
                    user: dataUser
                }
                modelUser.Update(dataUser, function () {
                    // todo, avisar del cambio al usuario?
                });
            });
        },
        edit: function (element, idProject) {
            require(['controllers/crud/projectMngr'], function (Controller) {
                (new Controller()).showEdit(element, idProject);
            });
        },
        addData: function (element) {
            var that = this;
            require(['controllers/crud/projectMngr'], function (Controller) {
                (new Controller()).addData(element, that.options.solutionId);
            });
        },
        mngProjects: function (element) {
            require(['controllers/crud/projectMngr'], function (Controller) {
                (new Controller()).show(element);
            });
        },
        close: function () {
            var key = $("#currentTask").attr("data-rel");
            require(['controllers/home/home'], function (Controller) {
                (new Controller()).removeTask(key);
            });
        },
        addTableHandlers: function () {
            this.$table.find("a.btn_edit").on("click", $.proxy(function (e) {
                this.edit(this.$el, +$(e.currentTarget).closest("tr").attr("data-id"));
                return false;
            }, this));
            this.$table.find("a.btn_remove").on("click", this.deleteData);
        },
        addHandlers: function () {
            this.$content.find("button.btn_add").on("click", $.proxy(function () {
                this.addData(this.$el, this.options.solutionId);
                return false;
            }, this));

            this.$content.find("div.abm-top-options a.btn-close").on("click", this.close);
        }
    });
    return view;
});